#! /bin/sh


# 运行当前的case，如果输出参数为文件夹，则运行其下以及其子目录下的所有case
#
# Args:
#   case: string:  case dir or case path
#   
# Returns:
#   string: with escaped character(s)
function _shunit_runCurrentCase() {
	for _shunit_curCase in $*
	do
		if [ -d $_shunit_curCase ]
		then
			_shunit_caseNames=`ls $_shunit_curCase`
			_shunit_runCurrentCase_Args=""
			for _shunit_caseName in $_shunit_caseNames
			do
				if [ -z "$_shunit_runCurrentCase_Args" ]
				then
					_shunit_runCurrentCase_Args="$_shunit_curCase/$_shunit_caseName"
				else
					_shunit_runCurrentCase_Args="$_shunit_runCurrentCase_Args $_shunit_curCase/$_shunit_caseName"
				fi
			done
			_shunit_runCurrentCase "$_shunit_runCurrentCase_Args"
		else
			if [ -f $_shunit_curCase ]
			then
				_shunit_dirName=`dirname $_shunit_curCase`
				_shunit_baseName=`basename $_shunit_curCase`
				
				if [[ "$_shunit_baseName" =~ "_test.sh" ]]
				#if [[ "$_shunit_baseName" =~ _test\.sh$ ]]
				then
					echo "info_shunit_baseName:$_shunit_baseName"
					echo "info_shunit_dirName:$_shunit_dirName"
					cd $_shunit_dirName && bash $_shunit_baseName
				fi
				
			fi
		fi
	done
}

. ../conf/shunit2.conf

PROJECT_DIR="$SHUNIT_SCRIPT_HOME"

:>$SHUNIT_REPORT_HOME/report.txt
_shunit_runCurrentCase $SHUNIT_SCRIPT_HOME | tee $SHUNIT_REPORT_HOME/report.txt 2>&1

gawk -v FS="\t" '
	$1=="shunit_report_info" {
		scripts[$2]=0;
		if ($2 in scripts_info) {
			scripts_info[$2] = (scripts_info[$2]"##"$3)
		} else {
			scripts_info[$2] = $3
		}
		if ($4 == "pass") {
			success_cases[$2, $3] = 0
		}
		
		if ($4 == "fail") {
			if ( $2""SUBSEP""$3 in fail_cases) {
				fail_cases[$2, $3] = fail_cases[$2, $3]"##"$5
			} else {
				fail_cases[$2, $3] = $5
			}
		}		
	}
	END {
		total_cases_cnt = 0
		success_cases_cnt = 0
		fail_cases_cnt = 0
		
		for (key in scripts) {
			cases_string = scripts_info[key]
			split(cases_string, cases_array, "##")
			
			split("", cases_map)
			for (case_index in cases_array) {
				cases_map[cases_array[case_index]] = 0
			}
			for (single_case in cases_map) {
				if (key""SUBSEP""single_case in fail_cases) {
					fail_cases_cnt++
					total_cases_cnt++
				} else if (key""SUBSEP""single_case in success_cases)  {
					success_cases_cnt++
					total_cases_cnt++
				} else {
					print "ERROR ", key, single_case
				}			
			}
			
		}
		
		print "Run "total_cases_cnt" cases"
		print "Success "success_cases_cnt" cases"
		print "Fail "fail_cases_cnt" cases"
		
		if (size_of_array(fail_cases) >= 1) {
			print "=======================fail case list=================="
			for (key in fail_cases) {
				split(key, keyValuePair, SUBSEP)
				print keyValuePair[1], keyValuePair[2], fail_cases[key]
			}
		}
		
		minus_array(success_cases, fail_cases, success_cases_array)
		if (size_of_array(success_cases_array) >= 1) {
			print "=======================success case list=================="
			for (key in success_cases_array) {
				split(key, keyValuePair, SUBSEP)
				print keyValuePair[1], keyValuePair[2]
			}
		}
	}
	
	function size_of_array(array, _ARG_END_, key, size) {
		size = 0
		for (key in array) {
			size++
		}
		return size
	}
	
	function minus_array(array1, array2, arrayReturn, _ARG_END_, key) {
		split("", arrayReturn)
		for (key in array1) {
			if (! (key in array2) ) {
				arrayReturn[key] = array1[key]
			}
		}
	}
' $SHUNIT_REPORT_HOME/report.txt 
